package cn.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 经典ABA问题 无法感知其他线程是否修改 只比较值
 */
@Slf4j
public class ABATest {
    private static AtomicReference<String> str;
    public static void main(String[] args) throws InterruptedException {
        str=new AtomicReference<>("A");
        other();
        Thread.sleep(1000);
        log.info("A->C    "+str.compareAndSet("A","c"));

    }
    static  void  other(){
        new Thread(()->{
            str.compareAndSet("A","B");
            log.info("???");
        }).start();
        new Thread(()->{
            str.compareAndSet("B","A");
        }).start();
    }
}
